import Vue from 'vue'
import VueRouter from 'vue-router'
import session from '../utils/session';
import Layout from '../views/layout';
import Login from '../views/login';

Vue.use(VueRouter)

const routes = [
  // 默认登陆页面
  {path:"",redirect:"/login"},
  // 登陆页面
  {path:"/login",component:Login},
  // 后台首页
  {path:"/home",meta:{title:"首页"},component:Layout,redirect:"/home/",children:[
    {path:"/",component:()=>import("../views/home/Home.vue")}
  ]},
]
// 路由对象创建
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 路由对象创建之后写导航守卫
router.beforeEach((to, from,next) => {
  let token=session.get("sell_token");
  if(token){
    next();
  }else{
    if(to.path=="/login"){
      next();
    }else{
      next("/login")
    }
  }
})

const menusList = [
  {path:"",redirect:"/login",meta:{isMenu:"no"}},
  // 登陆页面
  {path:"/login",component:Login,meta:{isMenu:"no"}},
  // 后台首页
  {path:"/home",meta:{title:"首页",icon:'icon-31shouyexuanzhong',level:1},component:Layout,redirect:"/home/",children:[
    {path:"/",component:()=>import("../views/home/Home.vue")}
  ]},
  // 订单管理都没有加roles，表示超级管理员和普通管理员都可以看
  {path:"/order",meta:{title:"订单管理",icon:'icon-dingdan1',level:2},component:Layout,redirect:"/order/list",children:[
    {path:"list",meta:{title:"订单列表"},component:()=>import("../views/order/OrderList.vue")},
    {path:"detail",meta:{title:"订单详情",isMenu:'no'},component:()=>import("../views/order/OrderDetail.vue")},
    {path:"update",meta:{title:"修改订单",isMenu:'no'},component:()=>import('../views/order/OrderUpdate.vue')},
  ]},
  // 商品
  {path:"/goods",meta:{title:"商品管理",icon:'icon-shangpin',level:2,roles:['super','normal']},component:Layout,redirect:"/goods/list",children:[
    {path:"list",meta:{title:"商品列表",roles:['super','normal']},component:()=>import("../views/goods/GoodsList.vue")},
    {path:"save",meta:{title:"添加商品",roles:['super']},component:()=>import("../views/goods/GoodsSave.vue")},
    {path:"type",meta:{title:"商品类型",roles:['super']},component:()=>import("../views/goods/GoodsType.vue")},
  ]},
  // 店铺
  {path:"/shop",meta:{title:"店铺管理",icon:'icon-dianpu',level:1,roles:['super']},component:Layout,redirect:"/shop/list",children:[
    {path:"",component:()=>import("../views/shop")}
  ]},
  // 用户管理
  {path:"/users",meta:{title:"用户管理",icon:'icon-zhanghaozhongxinzhanghaoguanli',level:2,roles:['super','normal']},component:Layout,redirect:"/users/list",children:[
    {path:"list",meta:{title:"用户列表",roles:['super']},component:()=>import("../views/users/UsersList.vue")},
    {path:"save",meta:{title:"添加用户",roles:['super']},component:()=>import("../views/users/UserSave.vue")},
    {path:"userpwd",meta:{title:"修改密码",roles:['super','normal']},component:()=>import("../views/users/UserPwd.vue")},
    {path:"personal",meta:{title:"个人中心",roles:['super']},component:()=>import("../views/users/UserPersonal.vue")},

  ]},
  // 统计
  {path:"/census",meta:{title:"统计管理",icon:'icon-tongjifenxi-changguitongji',level:2,roles:['super']},component:Layout,redirect:"/census/goods",children:[
    {path:"goods",meta:{title:"订单统计",roles:['super']},component:()=>import("../views/census/GoodScensus.vue")},
    {path:"order",meta:{title:"商品统计",roles:['super']},component:()=>import("../views/census/OrderCensus.vue")},
    
  ]},
  {path:"/404",meta:{isMenu:"no"},component:()=>import("../404/index.vue")},
  {path:"*",redirect:"/404",meta:{isMenu:"no"}},
]

// 定义一个计算路由的方法
export function headler(){
  let role=session.get("sell_role");//从本地获取当前登陆对象的角色信息，（判断是超级管理员还是普通管理）
  // role就是super，nomarl，如果有值，都给super
  // 如果不是普通或者超级管理员，默认以普通会员进行登陆
  if(!role){
    role="normal";
  }
  let menus=[];
  // 遍历menusList整个路由
  menusList.forEach((item)=>{
    // 调用isRole这个函数，并且有俩个实参，此时遍历是一级遍历
    if(isRole(item,role)){
      // 判断一级是否包含children,如果包含那么就遍历二级路由
      let ch=[]
      if(item.children){
        item.children.forEach((obj)=>{

          if(isRole(obj,role)){
            console.log(obj);
            ch.push(obj);
          }
        })
        // 在push之前把item中的children更新
        item.children=ch;
      }
      menus.push(item)
    }
  })
  router.addRoutes(menus)
  //排除不渲染到目录的菜单,把其他渲染的放在menus里
  let aa=[];
  menus.forEach((item)=>{
    if(item.meta && item.meta.isMenu!=='no'){
      if(item.children){
        let cc=[];
        item.children.forEach((obj)=>{
          if(obj.meta && obj.meta.isMenu!=='no'){
            cc.push(obj);
          }
        })
        item.children=cc;
      }
      aa.push(item);
    }
  })
  // 将过滤好的菜单aa放在本地,之后再菜单文件显示
  session.set("sell_menus",aa);
  console.log(menus);
}
headler();

// 用这个函数判断管理员角色，之后不同角色匹配不同路由，obj代表当前所遍历的对象，
function isRole(obj,role){
  // 判断当前路由对象有没有roles属性
  if(obj.meta && obj.meta.roles){
    // 在路由roles属性中是否包含了role的值，是则返回true
      return obj.meta.roles.includes(role);
  }else{
    // 没有roles就是管理员和普通用户都可以显示
    return true;
  }
}



export default router
